/**
 * 项目设置 列表页
 *
 * 作者 admin
 * 日期 2024-07-29 14:19:15
 */
layui.use(['jquery', 'laydate', 'febs', 'form', 'formSelects', 'table', 'commonJS', 'validate'], function () {
    var $ = layui.jquery,
        laydate = layui.laydate,
        febs = layui.febs,
        form = layui.form,
        table = layui.table,
        commonJS = layui.commonJS,
        validate = layui.validate,
        $view = $('#febs-baseProject'),
        $query = $view.find('#query'),
        $searchForm = $view.find('#baseProject-table-form'),
        $tenantSelect = $searchForm.find('select[name="tenantSelect"]'),
        $projectType = $searchForm.find('select[name="projectType"]'),
        $status = $searchForm.find('select[name="status"]'),
        tableIns;

    initDictItemSelect_projectList();
    form.verify(validate);
    form.render();

    initTable();

    function initTable() {
        tableIns = febs.table.init({
            elem: $view.find('table'),
            id: 'baseProjectTable',
            url: ctx + 'baseProject/list',
            where: getQueryParams(),
            height: getTableHeight(),
            skin: '',
            cols: [[
                {title: '序号', type: 'numbers',width: 60},
                // {field: 'tenantName', title: '公司名称', minWidth: 260,hide:!hasHeadCompanyRole},
                {field: 'projectTypeName',align:'center', title: '项目类型', width: 100},
                {field: 'projectName', title: '项目名称', minWidth: 180},
                {field: 'areaName', title: '项目区域', minWidth: 240, templet: function (d) {
                        var span = "";
                        if(d.areaName && d.areaName.length>0){
                            var nameList = d.areaName.split(",");
                            nameList.forEach(item=> {
                                span += "&nbsp;&nbsp;<span class='layui-badge febs-bg-green'>"+item+"</span>";
                            })
                        }
                        return span;
                    }
                },
                {field: 'projectManageName',align:'center', title: '项目负责人', width: 140},
                {field: 'safetyManageName',align:'center', title: '安全负责人', width: 140},
                {field: 'startDate',align:'center', title: '开始周期', width: 200,templet: function (d) {
                        return d.startDate + '~' +d.endDate;
                    }
                },
                {
                    field: 'status',align:'center', title: '项目状态', width: 100, templet: function (d) {
                        var flg = '';
                        if (d.status == '1') {
                            flg = '<span class="layui-badge febs-bg-cyan">在&nbsp;&nbsp;建</span>'
                        } else if (d.status == '2') {
                            flg = '<span class="layui-badge febs-bg-red">已交付</span>'
                        }
                        return flg;
                    }
                },
                {field: 'sort',align:'center', title: '排序', width: 60},
                {title: '操作',align:'center',  toolbar: '#baseProject-option', width: 160}
            ]],
            toolbar: '#baseProject-toolbar',
            defaultToolbar: []
        });
    }

    function initDictItemSelect_projectList(){
        // 项目类型
        febs.initDictItemSelect($projectType,'SAFETY_PROJECT_TYPE', '',null);
    }

    table.on('toolbar(baseProjectTable)', function (obj) {
        if (obj.event === 'add') {
            febs.modal.open('新增项目', 'baseProject/add', {
                btn: ['提交', '取消'],
                area: $(window).width() <= 750 ? ['95%', '85%']: ['70%', '85%'],
                yes: function (index, layero) {
                    $('#baseProject-edit').find('#submit').trigger('click');
                },
                btn2: function () {
                    layer.closeAll();
                },
                data: {
                    tenantId: hasHeadCompanyRole?$tenantSelect.val():currentTenantId,
                }
            });
        }
    });

    table.on('tool(baseProjectTable)', function (obj) {
        var data = obj.data,
            layEvent = obj.event;
        if (layEvent === 'del') {
            febs.modal.confirm('删除项目', '确定删除该项目？', function () {
                deleteIds(data.projectId);
            });
        }
        if (layEvent === 'edit') {
            febs.modal.open('修改项目', 'baseProject/update/' + data.projectId, {
                area: $(window).width() <= 750 ? ['95%', '85%']: ['70%', '85%'],
                btn: ['提交', '取消'],
                yes: function (index, layero) {
                    $('#baseProject-edit').find('#submit').trigger('click');
                },
                btn2: function () {
                    layer.closeAll();
                },
                data: {
                    tenantId: data.tenantId,
                },
            });
        }
    });

    function reloadPage(curr){
        let _curr = $view.find(".layui-laypage-em").next().html();
        if (!!curr) {
            _curr = curr;
        }
        tableIns.reload({where: getQueryParams(), page: {curr: _curr}});
    }

    $query.on('click', function () {
        reloadPage(1);
    });


    function getQueryParams() {
        return {
            tenantId: $tenantSelect.val(),
            projectType: $projectType.val(),
            projectName: $searchForm.find('input[name="projectName"]').val()?.trim(),
            status: $status.val(),
            invalidate_ie_cache: new Date()
        };
    }

    function deleteIds(ids) {
        febs.get(ctx + 'baseProject/delete/' + ids, null, function () {
            febs.alert.success('删除数据成功');
            $query.trigger('click');
        })
    }

    function getTableHeight() {
        return commonJS.calcTableHeight();
    }

    $(window).resize(function () {
        tableIns.reload({height: getTableHeight()});
    });
});